<template>
  <div class="box">
    <van-nav-bar
      title="设置"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="set_top">
      <div class="set_top_msg" @click="goChangeMsg">
        <img :src="avatar" alt="">
        <div>
          <p>{{userName}}</p>
          <p>用户ID:{{userTel}}</p>
        </div>
        <van-icon name="arrow" class="icon" />
      </div>
      <div class="set_top_zhaq" @click="goChangepwd">
        <p>账号安全</p>
        <van-icon name="arrow" class="icon" />
      </div>
    </div>
    <div class="set_bottom">
      <div @click="goYhxy">
        <p>用户协议</p>
        <van-icon name="arrow" class="icon" />
      </div>
      <div @click="goGyjc">
        <p>关于聚橙</p>
        <van-icon name="arrow" class="icon" />
      </div>
    </div>
    <div class="set_button">
      <button @click="quit">退出登录</button>
      <button @click="logout">注销账户</button>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { NavBar, Icon, Toast } from 'vant'
import { logoutMsg, getUserInfo } from '@/api'
Vue.use(NavBar)
Vue.use(Icon)
Vue.use(Toast)
export default {
  data () {
    return {
      avatar: 'https://m.juooo.com/static/img/logo-user.8413cbf.png',
      userName: localStorage.getItem('nickname'),
      userTel: localStorage.getItem('user')
    }
  },
  mounted () {
    getUserInfo({
      userid: localStorage.getItem('userid'),
      token: localStorage.getItem('token')
    }).then(res => {
      if (res.data.message === 'token验证失败') {
        Toast('您登录的用户信息已过期,请重新登录')
        localStorage.clear()
        setTimeout(() => {
          this.$router.replace('/login')
        }, 1000)
      } else {
        this.avatar = res.data.data[0].avatar === undefined ? 'https://m.juooo.com/static/img/logo-user.8413cbf.png' : res.data.data[0].avatar
      }
    })
  },
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    goChangeMsg () {
      this.$router.push('/changeMsg')
    },
    goChangepwd () {
      this.$router.push('/changePwd')
    },
    goYhxy () {
      window.location.href = 'https://m.juooo.com/static/html/disclaimer.html'
    },
    goGyjc () {
      window.location.href = 'https://m.juooo.com/Myjuooo/AboutUs'
    },
    quit () {
      localStorage.clear()
      this.$router.replace('/home')
    },
    logout () {
      logoutMsg({
        userid: localStorage.getItem('userid'),
        token: localStorage.getItem('token')
      }).then(res => {
        if (res.data.message === 'token验证失败') {
          Toast('您登录的用户信息已过期,请重新登录')
          localStorage.clear()
          setTimeout(() => {
            this.$router.replace('/login')
          }, 1000)
        } else {
          // console.log(res)
          localStorage.clear()
          this.$router.replace('/home')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.set_top {
  width: 100%;
  height: 1.62rem;
  background: white;
  border-bottom: 0.12rem solid #F5F5F5;
  .set_top_msg {
    box-sizing: border-box;
    width: 100%;
    height: 0.9rem;
    padding: 0.17rem 0.15rem;
    overflow: hidden;
    img {
      float: left;
      width: 0.55rem;
      height: 0.55rem;
      border: 1px solid #eeeeee;
      border-radius: 50%;
    }
    div {
      float: left;
      margin-left: 0.1rem;
      p:nth-of-type(1) {
        font-size: 0.14rem;
        line-height: 0.2rem;
        color: #232323;
        margin-top: 0.07rem;
      }
      p:nth-of-type(2) {
        font-size: 0.12rem;
        line-height: 0.16rem;
        color: #999999;
        margin-top: 0.07rem;
      }
    }
    .icon {
      float: right;
      line-height: 0.55rem;
    }
  }
  .set_top_zhaq {
    height: 0.6rem;
    margin: 0 0.15rem;
    border-top: 1px solid #EBEBEB;
    p {
      line-height: 0.6rem;
      float: left;
      font-size: 0.16rem;
    }
    .icon {
      float: right;
      line-height: 0.6rem;
    }
  }
}
.set_bottom {
  width: 100%;
  height: 1.32rem;
  background: white;
  border-bottom: 0.12rem solid #F5F5F5;
  div:nth-of-type(1) {
    border-bottom: 1px solid #EBEBEB;
  }
  div {
    height: 0.6rem;
    margin: 0 0.15rem;
    p {
      line-height: 0.6rem;
      float: left;
      font-size: 0.16rem;
    }
    .icon {
      float: right;
      line-height: 0.6rem;
    }
  }
}
.set_button {
  height: 0.47rem;
  margin: 0.4rem 0.15rem 0 0.15rem;
  button {
    float: left;
    border: none;
    width: 50%;
    height: 0.47rem;
    text-align: center;
    line-height: 0.47rem;
    color: #ff6742;
    background: #F5F5F5;
  }
  button:nth-of-type(1) {
    border-radius: 0.23rem 0 0 0.23rem;
  }
  button:nth-of-type(2) {
    border-radius: 0 0.23rem 0.23rem 0;
  }
}
/deep/ .van-icon-arrow-left {
  font-size: 0.2rem;
  color: #666666;
}
/deep/ .van-hairline--bottom {
  border-bottom: 1px solid #EBEBEB;
}
</style>
